package com.jshoperxms.service.impl;

import com.jshoperxms.controller.mall.backstage.vo.coupon.CouponTCodeVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.CouponEnums;
import com.jshoperxms.controller.utils.maths.Arith;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.dao.CouponCodeTDao;
import com.jshoperxms.dao.CouponTDao;
import com.jshoperxms.dao.MemberCouponTDao;
import com.jshoperxms.entity.CouponCodeT;
import com.jshoperxms.entity.CouponT;
import com.jshoperxms.entity.MemberCouponT;
import com.jshoperxms.service.CouponCodeTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Random;

/**
 * Created by sdywcd on 17/1/10.
 * Des:
 */

@Service("couponCodeTService")
@Scope("prototype")
public class CouponCodeTServiceImpl extends BaseTServiceImpl<CouponCodeT> implements CouponCodeTService {



    @Resource
    private CouponTDao couponTDao;

    @Resource
    private CouponCodeTDao couponCodeTDao;


    @Resource
    private MemberCouponTDao memberCouponTDao;

    @Resource
    private Serial serial;


    /**
     * 根据rangetype生成不同类型的优惠券编码
     * @param couponTCodeVo
     */
    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void saveCouponCodeTransactional(CouponTCodeVo couponTCodeVo) {
        //检查优惠券基本信息是否存在
        CouponT coupont=this.couponTDao.findByPK(CouponT.class,couponTCodeVo.getCouponid());
        if(coupont!=null){
            //添加优惠券代码
            for(int i=0;i<couponTCodeVo.getTotal();i++){
                CouponCodeT bean=new CouponCodeT();
                bean.setId(this.serial.Serialid(SerialKeys.COUPONCODE));
                bean.setRangetype(couponTCodeVo.getRangetype());
                bean.setBindmemberid(StaticKey.EMPTY);
                bean.setCode(bean.getId()+getRandomCode(Integer.MAX_VALUE,i));
                bean.setCouponid(coupont.getId());
                bean.setCreatetime(DateTools.getSystemTimeBi());
                bean.setCreatorid(BaseTools.getAdminCreateId());
                bean.setStatus(couponTCodeVo.getStatus());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(1);
                this.couponCodeTDao.save(bean);
            }
            //更新优惠券基本信息
            coupont.setTotal(couponTCodeVo.getTotal());
            coupont.setUpdatetime(DateTools.getSystemTimeBi());
            this.couponTDao.update(coupont);
        }

    }


    /**
     * 删除优惠券代码
     * @param couponTCodeVo
     */
    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delCouponCodeTransactional(CouponTCodeVo couponTCodeVo) {
        //删除优惠券代码
        String ids[]=StringUtils.split(couponTCodeVo.getId(),StaticKey.SPLITDOT);
        for(String s:ids){
            CouponCodeT bean=this.couponCodeTDao.findByPK(CouponCodeT.class,s);
            if(bean!=null){
                bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                bean.setVersiont(bean.getVersiont()+1);
                this.couponCodeTDao.update(bean);
            }
        }
        //删除会员获得的优惠券信息
        for(String s:ids){
            Criterion criterion= Restrictions.eq("couponcodeid",s);
            MemberCouponT memberCouponT=this.memberCouponTDao.findOneByCriteria(MemberCouponT.class,criterion);
            if(memberCouponT!=null){
                memberCouponT.setStatus(BaseEnums.DataUsingState.DEL.getState());
                memberCouponT.setVersiont(memberCouponT.getVersiont()+1);
                this.memberCouponTDao.update(memberCouponT);
            }
        }
        //更新优惠券基础信息
        CouponT couponT=this.couponTDao.findByPK(CouponT.class,couponTCodeVo.getCouponid());
        if(couponT!=null){
            couponT.setTotal(Arith.sub((double)couponT.getTotal(),(double)ids.length).intValue());
            couponT.setUpdatetime(DateTools.getSystemTimeBi());
            couponT.setVersiont(couponT.getVersiont()+1);
            this.couponTDao.update(couponT);
        }


    }


    private int getRandomCode(int max,int min){
        Random random = new Random();
        return random.nextInt(max)%(max-min+1) + min;
    }

}
